/*
 * 文件名：OrgProQaController.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wuzhiyang
 * 修改时间：2020年3月19日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.mgr.sts.web;


import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import top.infopub.generic.GenericController;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.hr.service.OrgInfoService;
import top.infopub.mgr.sts.domain.OrgProQaDomain;
import top.infopub.mgr.sts.service.OrgProQaService;
import top.infopub.model.hr.OrgInfo;
import top.infopub.security.util.SessionUserUtils;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;


/**
 * 项目刚性指标
 * @author wuzhiyang
 * @version 2020年3月19日
 * @see OrgProQaController
 * @since
 */
@Controller
@RequestMapping("/orgProQa")
public class OrgProQaController extends GenericController {

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private OrgProQaService orgProQaService;

    /**
     * 列表页
     * @return
     */
    @RequestMapping("/list")
    public String list(HttpServletRequest request) {
        String visibleOrgIds = (String)SessionUserUtils.getSessionAttribute("visibleOrgIds");
        List<OrgInfo> orgInfos = new ArrayList<OrgInfo>();
        if (StringUtils.isEmpty(visibleOrgIds)) {
            Map<String, Object> where = Maps.newHashMap();
            where.put("state", "1");
            where.put("orderCondition", " types asc ");
            orgInfos = orgInfoService.selectByMapWhere(where);
        }
        else {
            orgInfos = orgInfoService.selectByVisibleIds(visibleOrgIds);
        }
        request.setAttribute("orgInfos", orgInfos);
        return "sts/lay-orgProQa-list";
    }

    /**
     * 根据条件获取获取列表数据
     * @param stsMonthStart 开始月份
     * @param stsMonthEnd 结束月份
     * @param orgTypeAndId ${org.id}-${org.types}
     * @param orgProName 项目名称
     * @return 
     * @throws ParseException 
     * @see
     */
    @RequestMapping("/queryDataByCondition")
    public @ResponseBody RspVo<OrgProQaDomain> queryDataByCondition(String stsMonthStart,
                                                                    String stsMonthEnd,
                                                                    String orgTypeAndId,
                                                                    String orgProName,
                                                                    ParamPage paramPage)
        throws ParseException {
        if (StringUtils.isBlank(stsMonthStart) && StringUtils.isBlank(stsMonthEnd)) {
            return RspVo.getFailureResponse(-1, "请选择统计月份~");
        }
        if (StringUtils.isBlank(orgTypeAndId)) {
            return RspVo.getFailureResponse(-1, "请选择公司主体~");
        }
        Date monthStart = DateUtils.parseDate(stsMonthStart, "yyyy年MM月");
        Date monthEnd = DateUtils.parseDate(stsMonthEnd, "yyyy年MM月");
        String orgId = orgTypeAndId.split("-")[0];
        String type = orgTypeAndId.split("-")[1];
        PageHelper.startPage(paramPage.getPage(), paramPage.getLimit());
        List<OrgProQaDomain> orgProQaDomains = orgProQaService.selectQaByWhere(monthStart,
            monthEnd, type, orgId, orgProName);
        for (OrgProQaDomain orgProQaDomain : orgProQaDomains) {
            String[] curMonthFinishTtsTotal = orgProQaDomain.getCurMonthFinishTtsTotal().split(
                "\\,");
            if (curMonthFinishTtsTotal != null && curMonthFinishTtsTotal.length > 0) {
                for (String curMonthFinishTts : curMonthFinishTtsTotal) {
                    String[] split = curMonthFinishTts.split("\\=");
                    if ("1".equals(split[0])) {
                        orgProQaDomain.setCurMonthFinishTts1(new BigDecimal(split[1]));
                    }
                    else if ("2".equals(split[0])) {
                        orgProQaDomain.setCurMonthFinishTts2(new BigDecimal(split[2]));
                        orgProQaDomain.setCurMonthFinish2(new BigDecimal(split[1]));
                        orgProQaDomain.setCurMonthFinishHms2(new BigDecimal(split[3]));
                    }
                    else if ("3".equals(split[0])) {
                        orgProQaDomain.setCurMonthFinishTts3(new BigDecimal(split[2]));
                        orgProQaDomain.setCurMonthFinish3(new BigDecimal(split[1]));
                        orgProQaDomain.setCurMonthFinishHms3(new BigDecimal(split[3]));
                    }
                    else if ("4".equals(split[0])) {
                        orgProQaDomain.setCurMonthFinishTts4(new BigDecimal(split[2]));
                        orgProQaDomain.setCurMonthFinish4(new BigDecimal(split[1]));
                        orgProQaDomain.setCurMonthFinishHms4(new BigDecimal(split[3]));
                    }
                    else if ("5".equals(split[0])) {
                        orgProQaDomain.setCurMonthFinishTts5(new BigDecimal(split[1]));
                    }
                    else if ("6".equals(split[0])) {
                        orgProQaDomain.setCurMonthFinishTts6(new BigDecimal(split[1]));
                    }
                    else if ("7".equals(split[0])) {
                        orgProQaDomain.setCurMonthFinishTts7(new BigDecimal(split[1]));
                    }
                    else if ("8".equals(split[0])) {
                        orgProQaDomain.setCurMonthFinishTts8(new BigDecimal(split[1]));
                    }
                    else if ("9".equals(split[0])) {
                        orgProQaDomain.setCurMonthFinishTts9(new BigDecimal(split[1]));
                    }
                    else if ("10".equals(split[0])) {
                        orgProQaDomain.setCurMonthFinishTts10(new BigDecimal(split[1]));
                    }
                }
            }
        }
        Page<OrgProQaDomain> page = (Page<OrgProQaDomain>)orgProQaDomains;
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }
}